﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SmartSpace.Data;

namespace SsTool.Metadata.Windows
{
    /// <summary>
    /// Interaction logic for CreateEntityMetadataWindow.xaml
    /// </summary>
    public partial class CreateEntityMetadataWindow : Window
    {
        public EntityMetadata EntityMetadata { get; private set; }

        public CreateEntityMetadataWindow()
        {
            InitializeComponent();

            IDataService dataService = App.Get<IDataService>();

            EntityMetadata = new EntityMetadata();
            EntityMetadata.Id = Guid.NewGuid();
            EntityMetadata.Properties = new List<SmartSpace.Data.PropertyMetadata>();
            #region 添加系统字段
            string[] pros = new string[] { 
                "__Id,标识,{1722EECC-FCB0-4FF9-9F47-DD8EF67AD3E2},true,true", 
                "__Name,名称,{6C89BE2E-2EC5-46CD-9E6C-BE6E50438007},false,true", 
                "__Creator,创建人,{83AEF0ED-CC1E-487B-8418-DBBE1276B5AF},false,true", 
                "__CreatedDate,创建时间,{FD8D0D23-8A82-4EE8-80C1-5BA44467B2CE},false,true"};
            for (int index = 0; index < pros.Length; index++)
            {
                string[] items = pros[index].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                SmartSpace.Data.PropertyMetadata pro1 = new SmartSpace.Data.PropertyMetadata();
                pro1.Id = Guid.NewGuid();
                pro1.IsIdentity = Convert.ToBoolean(items[3]);
                if (pro1.IsIdentity)
                {
                    // 标识字段不能为空
                    pro1.IsNullable = false;
                }
                else
                {
                    pro1.IsNullable = true;
                }
                pro1.Name = items[0];
                pro1.Caption = items[1];
                pro1.IsSystem = Convert.ToBoolean(items[4]);
                pro1.EntityId = EntityMetadata.Id;
                TypeMetadata type = dataService.FindType(new Guid(items[2]));
                pro1.Type = type;
                pro1.TypeId = type.Id;
                pro1.MaxLength = type.MaxLength;
                pro1.MinLength = type.MinLength;
                pro1.OrderIndex = index;
                EntityMetadata.Properties.Add(pro1);
            }
            #endregion

            this.DataContext = EntityMetadata;
        }

        public CreateEntityMetadataWindow(EntityMetadata entity)
        {
            InitializeComponent();

            EntityMetadata = entity;
            this.DataContext = EntityMetadata;
        }

        private void _ctlBtnSubmit_Click(object sender, RoutedEventArgs e)
        {
            IDataService service = App.Get<IDataService>();
            if (service.StoreEntity(EntityMetadata) == true)
            {
                DialogResult = true;
            }
        }

        private void _ctlBtnCreate_Click(object sender, RoutedEventArgs e)
        {
            CreatePropertyWindow window = new CreatePropertyWindow(EntityMetadata);
            if (window.ShowDialog() == true)
            {
                if (EntityMetadata.Properties == null)
                {
                    EntityMetadata.Properties = new List<SmartSpace.Data.PropertyMetadata>();
                }
                EntityMetadata.Properties.Add(window.PropertyMetadata);
                this.DataContext = null;
                this.DataContext = EntityMetadata;
            }
        }

        private void _ctlBtnUpdate_Click(object sender, RoutedEventArgs e)
        {
            SmartSpace.Data.PropertyMetadata property = _ctlGridProperty.SelectedItem as SmartSpace.Data.PropertyMetadata;
            if (property != null)
            {
                //CreatePropertyWindow window = new CreatePropertyWindow(property);
                //if (window.ShowDialog() == true)
                //{
                //    this.DataContext = null;
                //    this.DataContext = EntityMetadata;
                //}
            }
        }
    }
}
